/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.google.common.collect.Maps;
import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Manager;
import com.guoyu.project.model.enums.ManagerLevel;
import com.guoyu.project.service.ManagerService;

/**
 * 管理员,前端接口ManagerController
 *
 * @author: xblibo Sun Jun 07 17:40:03 CST 2015
 * @version 1.0
 */
@Controller
@RequestMapping("admin/manager")
public class ManagerController {

    private final static Logger logger = LoggerFactory.getLogger(ManagerController.class);

    @Resource
    private ManagerService managerService;

    @RequestMapping("index")
    public String index() {
        return "admin/manager/index";
    }

    /**
     * 管理员列表页
     *
     * @return
     */
    @RequestMapping("list")
    public APIResponse<Pagination<Manager>> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        Pagination<Manager> pagination = managerService.queryManagers(offset, limit, params);
        return APIResponse.returnSuccess(pagination);

    }

    /**
     * 跳到添加管理员填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        model.addAttribute("levels", ManagerLevel.values());
        return "admin/manager/add";
    }

    /**
     * 保存管理员信息
     *
     * @return
     */
    @RequestMapping("save")
    public APIResponse save(Manager manager) {
        logger.info("提交保存管理员信息：{}", JsonUtil.encode(manager));
        managerService.saveManager(manager);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改管理员信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(int managerId, Model model) {
        Manager manager = managerService.queryById(managerId);
        model.addAttribute("manager", manager);
        model.addAttribute("levels", ManagerLevel.values());
        return "admin/manager/edit";
    }

    /**
     * 更新管理员信息
     *
     * @return
     */
    @RequestMapping("update")
    public APIResponse update(Manager manager) {
        logger.info("提交修改管理员信息：{}", JsonUtil.encode(manager));
        managerService.updateManager(manager);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除管理员信息
     *
     * @return
     */
    @RequestMapping("delete")
    public APIResponse delete(int managerId) {
        managerService.deleteById(managerId);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除管理员信息
     *
     * @return
     */
    @RequestMapping("modify")
    public String modify() {
        return "admin/manager/modify";
    }
}